package name.sraynal.srt;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SrtExpander extends Main {
	private static final String DATE_FORMAT = "KK:mm:ss,SSS";
	private static Pattern LINE_PATTERN = Pattern.compile("[ ]*(\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d)[ ]+-->" +
															"[ ]+(\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d)[ ]*");
	private String fileName;
	private double timeLengthRatio;
	private long start;
	
	public SrtExpander(String fileName) {
		super();
		this.fileName = fileName;
		output("Please input the final subtitle outcome time in the following form : " + DATE_FORMAT);
		String finalTime = readInput();
		try {
			setTimeValues(finalTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		
		SrtExpander mod = new SrtExpander(args[0]);
		mod.run();
	}

	public void run() throws Exception {
		File file = new File(fileName);
		File dir = new File("C:\\out");
		dir.mkdirs();
		File out = new File(dir, file.getName());
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
		BufferedReader reader = new BufferedReader(new FileReader(file));
		BufferedWriter writer = new BufferedWriter(new FileWriter(out));
		String read;
		
		while ((read = reader.readLine()) != null) {
			Matcher matcher = LINE_PATTERN.matcher(read);
			
			if (matcher.matches()) {
				Date current = format.parse(matcher.group(1));
				long newTime = (long) ((current.getTime() - start) * timeLengthRatio);
				current.setTime(start + newTime);
				writer.append(format.format(current)).append(" --> ");
				current = format.parse(matcher.group(2));
				newTime = (long) ((current.getTime() - start) * timeLengthRatio);
				current.setTime(start + newTime);
				writer.append(format.format(current)).append("\r\n");
			} else {
				writer.append(read);
				writer.newLine();
			}
		}
		
		reader.close();
		writer.close();
		output("Replace ? default N.");
		String resp = readInput();
		if (resp.toLowerCase().startsWith("y")) {
			file.renameTo(new File(file.getParentFile(), file.getName() + "old"));
			out.renameTo(file);
			output("Created output file : " + file);
		} else {
			output("Created output file : " + out);			
		}
		String command = "explorer " + out.getParent();
		Runtime.getRuntime().exec(command);
	}
	

	private void setTimeValues(String finalTime) throws Exception {
		File file = new File(fileName);
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String read;
		boolean isFirstOutcome = true;
		Date current = null;
		start = 0;
		
		while ((read = reader.readLine()) != null) {
			Matcher matcher = LINE_PATTERN.matcher(read);
			
			if ((matcher.matches()) && isFirstOutcome) {
				isFirstOutcome = false;
				start = format.parse(matcher.group(1)).getTime();
			}
			
			if (matcher.matches()) {
				current = format.parse(matcher.group(1));
			}
		}
		long stop = current.getTime();
		long oldTimeLength = stop - start;
		long newTimeLength = format.parse(finalTime).getTime() - start;
		timeLengthRatio = ((double) newTimeLength)/((double) oldTimeLength);
	}
}
